<?php
/**
 * CK框架 cookie类
 * @author wujibing<283109896@qq.com>
 * @version   1.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class cookie
{
	/**
	 * 赋值一个Cookie
	 * @access public static
	 * @param string $name
	 * @param string $value
	 * @param int $expire
	 * @param string $path
	 * @param string $domain
	 */
	public static function set($name,$value,$expire='',$path='',$domain='',$isClient=0)
	{
		if(!$expire) 
		{
            $expire = config::$configObject->COOKIE->COOKIE_EXPIRE;
        }
        if(empty($path)) 
        {
            $path = config::$configObject->COOKIE->COOKIE_PATH;
        }
        if(empty($domain)) 
        {
            $domain = config::$configObject->COOKIE->COOKIE_DOMAIN;
        }
        $expire = !empty($expire)? ($isClient ? $expire : SYSTIME + $expire) : 0;
        $value   =  base64_encode($value);
        $cookieName = config::$configObject->COOKIE->COOKIE_PREFIX.$name;
        setcookie($cookieName,$value,$expire,$path,$domain);
        $_COOKIE[$cookieName] = $value;
	}
	/**
	 * 获得一个Cookie
	 * @access public static
	 * @param string $name
	 * @return string
	 */
	public static function get($name)
	{
		$cookieName = config::$configObject->COOKIE->COOKIE_PREFIX.$name;
		return isset($_COOKIE[$cookieName]) ? base64_decode($_COOKIE[$cookieName]) : false;
	}
	/**
	 * 删除一个Cookie
	 * @access public static
	 * @param string $name
	 * @return string
	 */
	public static function del($name)
	{
		self::set($name,0,SYSTIME - 92600);
		$_COOKIE[config::$configObject->COOKIE->COOKIE_PREFIX.$name] = '';
	}
	/**
	 * 删除Cookie
	 * @access public static
	 */
	public static function clear()
	{
		if($_COOKIE)
		{
			$prefix = config::$configObject->COOKIE->COOKIE_PREFIX;
			foreach ($_COOKIE as $key => $value)
			{
				cookie::del(str_replace($prefix, '', $key));
			}
		}
	}
}